function QuerySuggestion(jqueryPath){
	var me=this;
	me.lastWord="";
	me.item=$(jqueryPath);
	me.queryText=$(me.item).find("[name='q']");
	me.module=$(me.item).find("[name='k']");
	me.suggestionDiv=$(me.item).find("#qSuggestion");
	me.suggestionActive=false;
	me.hideSuggestion=function(){
		if(me.suggestionActive)return;
		me.suggestionDiv.hide();
	};
	//搜索内容改变时，触发搜索建议方法
	me.queryText.bind('keyup',function(event){
		var e = event ;
		var inter = false;//控制列表inter的词不推荐
		 if(e && e.keyCode==38){ // 按 上
			 var selectIndex=-1;
				me.suggestionDiv.find("li").each(function(index){
					if($(this).attr("class")=="selected")selectIndex=index-1;
					
				 });
				if(selectIndex!=-1){
					if(selectIndex>=0){
						me.suggestionDiv.find("li").eq(selectIndex).select();
					}
				}else{
					me.suggestionDiv.find("li:last").select();
				}
		 }else if(e && e.keyCode==40){			//按下
			var selectIndex="";
			me.suggestionDiv.find("li").each(function(index){
				if($(this).attr("class")=="selected")selectIndex=index+1;
			 });
			var liLength=me.suggestionDiv.find("li").length;
			if(selectIndex>-1&&selectIndex<liLength)
				me.suggestionDiv.find("li").eq(selectIndex).select();
			else {
				me.suggestionDiv.find("li:first").select();
			}
		 }else if(e && e.keyCode==13){			//回车
			 inter = true;
			 if(me.suggestionDiv.css("display")=="none"||me.suggestionDiv.find("li.selected").length==0){
				 $("#queryForm").submit();
			 }else{
				me.suggestionDiv.find("li.selected").click();
			 }
		 }else if(e && e.keyCode==27){
			 me.suggestionDiv.hide();
		 }
		 if(!inter)
			 searchSuggestion();
	});
	
	//单击搜索框实，触发搜索建议方法
	me.queryText.bind('click',function(){
		me.lastWord="";
		searchSuggestion();
	});
	me.suggestionDiv.bind('select',function(){
		me.suggestionActive=true;
	});
	me.suggestionDiv.bind('mouseover',function(){
		me.suggestionActive=true;
	});
	me.suggestionDiv.bind('mouseout',function(){
		me.suggestionActive=false;
	});
	//失焦时隐藏查询框
	me.queryText.bind('blur',function(){
		me.hideSuggestion();
	});
	//点击输入框外部隐藏查询框
	$("body").click(function(e){
		if($(e.target).is('input:text'))
            return;
        else
        	me.hideSuggestion();
	});  
	function getLi (li) {
		var value=$(li).attr("word");
		q.queryText.val(value);
		q.suggestionActive=false;
		q.hideSuggestion();
	}
	function searchSuggestion(){
		var searchword=$(me.queryText).val()||"";
		var module=$(me.module).val();
		if(searchword==""){
			me.suggestionDiv.hide();
			return;
		}else if(me.lastWord==searchword){
			return;
		}else {
			me.lastWord=searchword;
			$.ajax({
				type: "POST",
		        url: "qSuggestion.jsp",
		        dataType: "html",
		        data:	{'searchword': searchword,'module':module},
		        success: function(html){
		        		if(html.indexOf("li")==-1)return;
				        me.suggestionDiv.html("");
				        me.suggestionDiv.append(html);
				        me.suggestionDiv.show();
				        //绑定点击事件
				        me.suggestionDiv.find("li").bind('click',function(){
				    		getLi(this);
				    	});
				        me.suggestionDiv.find("li").bind('select',function(){
				        	var thisli=this;
				        	me.suggestionDiv.find("li").each(function(index){
				        		me.suggestionDiv.find("li").eq(index).removeClass("selected");
				        		me.suggestionDiv.find("li").eq(index).removeClass("mouseover");
				        	});
				        	$(thisli).addClass("selected");
				        });
				        me.suggestionDiv.find("li").bind('mouseover',function(){
				        	var thisli=this;
				        	me.suggestionDiv.find("li").each(function(index){
				        		me.suggestionDiv.find("li").eq(index).removeClass("selected");
				        		me.suggestionDiv.find("li").eq(index).removeClass("mouseover");
				        	});
				        	$(thisli).addClass("mouseover");
				        });
		             }
			});
		}
	}
}
var q=new QuerySuggestion(".d");

